home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_315 / surf / bezpt.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  167 lines

  1. #include "mytypes.h"
  2. #include "scrnio.h"
  3. #include "control.h"
  4. #include "bezpt.h"
  5.  
  6. BezCoord Bezpt[MaxSegs+1];
  7.  
  8. int NumBezPts;
  9. int ActSeg;
  10. bool SegDrawn;
  11. int BezMesh = DefBezMeshVal;
  12. float BezStepSize = 1.0/DefBezMeshVal;
  13.  
  14. void SetBezMesh( value )
  15.     int value;
  16. {
  17.     BezMesh = value;
  18.     BezStepSize = 1.0/value;
  19. }
  20.  
  21.  
  22. void ClearSegments()
  23. {
  24.     NumBezPts = 0;
  25.     ActSeg = 0;
  26. }
  27.  
  28.  
  29. static  float xa, xb, xc, xd;
  30. static  float ya, yb, yc, yd;
  31. /*
  32.  * start up calculations that must be performed before calling
  33.  * CalcBezPt on any given segment
  34.  */
  35. void InitCalcBez()
  36. {
  37.         xa = -StartPtX(ActSeg) + 3.0*( Cntrl1X(ActSeg) - Cntrl2X(ActSeg))
  38.             + EndPtX(ActSeg);
  39.         xb = 3.0 *( StartPtX(ActSeg) + Cntrl2X(ActSeg) - 2.0*Cntrl1X(ActSeg));
  40.         xc = 3.0*( Cntrl1X(ActSeg) - StartPtX(ActSeg));
  41.         xd = StartPtX(ActSeg);
  42.  
  43.         ya = -StartPtY(ActSeg) + 3.0*( Cntrl1Y(ActSeg) - Cntrl2Y(ActSeg))
  44.             + EndPtY(ActSeg);
  45.         yb = 3.0 *( StartPtY(ActSeg) + Cntrl2Y(ActSeg) - 2.0*Cntrl1Y(ActSeg));
  46.         yc = 3.0*( Cntrl1Y(ActSeg) - StartPtY(ActSeg));
  47.         yd = StartPtY(ActSeg);
  48. }
  49.  
  50. /*
  51.  * calculate a point on the bezier curve of a segment
  52.  */
  53. void CalcBezPt( t, xvp, yvp)
  54.     float t;
  55.     float *xvp, *yvp;
  56. {
  57.     *xvp = (( t*xa + xb) * t + xc) *t + xd;
  58.  
  59.     *yvp = (( t*ya + yb) * t + yc) *t + yd;
  60. }
  61.  
  62.  
  63.  
  64. void XdrawBezSeg()
  65. {
  66.     float t;
  67.     float ftox, ftoy;
  68.     int fromx, fromy, tox, toy;
  69.  
  70.     InitCalcBez();
  71.     for( fromx = StartPtX(ActSeg), fromy = StartPtY(ActSeg), t=BezStepSize;
  72.         t < 1.0; fromx = tox, fromy = toy, t+= BezStepSize ) {
  73.  
  74.         CalcBezPt( t, &ftox, &ftoy );
  75.         tox = (int)ftox;
  76.         toy = (int)ftoy;
  77.         DrawLine( fromx, fromy, tox, toy, XOR );
  78.         DrawPnt( tox, toy, XOR );
  79.     }
  80.     DrawLine( fromx, fromy, EndPtX(ActSeg), EndPtY(ActSeg),XOR);
  81. }
  82.  
  83.  
  84.  
  85. void XdrawAllBezSegs()
  86. {
  87.     ResetActSeg();
  88.     do {
  89.         XdrawBezSeg();
  90.         NextSeg();
  91.     } while( ActSeg);
  92.  
  93.     DrawStartPt();      /* Leonards changes */
  94.     DrawEndPt();        /* Leonards changes */
  95.     DrawControl0();
  96.     DrawControl1();
  97. }
  98.  
  99.  
  100.  
  101. void ResetCurve()
  102. {
  103.     if( NumBezPts > 0 && CurMode == FITBEZIER ) {
  104.         int i;
  105.  
  106.         ClrWindow(true);
  107.         for( i = 0; i < NumBezPts; i++ ) {
  108.             Bezpt[i].x.cur1 = Bezpt[i].x.prev2 = Bezpt[i].x.cur0;
  109.             Bezpt[i].y.cur1 = Bezpt[i].y.prev2 = Bezpt[i].y.cur0;
  110.         }
  111.         ActSeg = 0;
  112.  
  113.         XdrawAllBezSegs();
  114.     }
  115. }
  116.  
  117.  
  118. /*
  119.  * set the value of a bezpt element
  120.  */
  121. static void SetBezPt( xval, yval )
  122.     int xval, yval;
  123. {
  124.     BezVal *i;
  125.  
  126.     i = &Bezpt[NumBezPts-1].x;
  127.     i->cur0 = i->prev2 = i->cur1 = xval;
  128.     i = &Bezpt[NumBezPts-1].y;
  129.     i->cur0 = i->prev2 = i->cur1 = yval;
  130. }
  131.  
  132.  
  133.  
  134.  
  135. void InitBezPt(xval,yval)
  136. int xval, yval;
  137. {
  138.     int segno;
  139.  
  140.     NumBezPts++;
  141.     SetBezPt(xval,yval);
  142.     segno = NumBezPts -2;
  143.     if( segno >= 0) {
  144.  
  145.         DrawLine( StartPtX( segno), StartPtY(segno),
  146.                 EndPtX( segno), EndPtY(segno), XOR );
  147.     }
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. void EditBezPt(xval, yval)
  155. {
  156.     int segno = NumBezPts -2;
  157.  
  158.     DrawLine( StartPtX(segno), StartPtY(segno),
  159.           EndPtX( segno ), EndPtY(segno), XOR );
  160.  
  161.     SetBezPt(xval, yval);
  162.  
  163.     DrawLine( StartPtX( segno), StartPtY(segno),
  164.           EndPtX( segno), EndPtY(segno), XOR );
  165. }
  166.  
  167.